package com.shengqi.proxy_hook.hook.activityhook;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.LayoutInflaterCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.shengqi.proxy_hook.R;

public class MainActivity extends Activity {
    private static final String TAG = "MainActivity";
    private Button btn_start_by_activity;
    private Button btn_start_by_context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        LayoutInflater layoutInflater = LayoutInflater.from(this);
        LayoutInflaterCompat.setFactory2(layoutInflater, new LayoutInflater.Factory2() {
            @Override
            public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {

                if ("Button".equalsIgnoreCase(name)) {
                    Log.e("TAG", "拦截view的创建 name = " + name + ", attrs = " + attrs.getAttributeValue(0));
                    for (int i = 0; i < attrs.getAttributeCount(); i++) {
                        Log.e("TAG","attrs = " + attrs.getAttributeName(i) + " = "+attrs.getAttributeValue(i));
                    }
//                    TextView textView = new TextView(MainActivity.this);
//                    textView.setText("拦截！！！！！");
                    Button btn = new Button(MainActivity.this, attrs);
                    btn.setText("拦截！！！！");
                    return btn;
                }
                return null;
            }

            @Override
            public View onCreateView(String name, Context context, AttributeSet attrs) {
                return null;
            }
        });

        //一定要放在下面否则报错 super.onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        //TODO： hook开关在这里
        SetTextHookHelper.doTextHook(this);
        setContentView(R.layout.activity_main);

        PermisionUtils.verifyStoragePermissions(this);

        // hook Activity.startActivity()的方法时不知道这行代码为什么放在attachBaseContext里面无效,
        // 调试发现,被hook的Instrumentation后来又会被替换掉原来的,
        // 猜想可能是attachBaseContext应该是在Activity的attach方法之前执行了。
//        ActivityThreadHookHelper.doActivityStartHook(this);
        btn_start_by_activity = findViewById(R.id.btn_start_by_activity);
        btn_start_by_activity.setText(R.string.activity_start_activity);
//
        btn_start_by_context = findViewById(R.id.btn_start_by_context);
        btn_start_by_context.setText(R.string.context_start_activity);
        Log.e(TAG, "mResources3 = " + getResources() + " " + getResources().getText(R.string.activity_start_activity));

        Log.e(TAG, "btn_start_by_context text = " + getResources().getResourceName(R.string.context_start_activity));
        btn_start_by_activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                startActivity(intent);
            }
        });

        btn_start_by_context.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, OtherActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                getApplicationContext().startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();
        LanguageManager.doTest();
        Log.e(TAG, "mResources array = " + getResources() + " " + getResources().getStringArray(R.array.futures_rank5)[0]);
        Log.e(TAG, "mResources array = " + getResources() + " " + getResources().getStringArray(R.array.futures_rank5)[1]);
    }


    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
        SetTextHookHelper.doTextHook(this);
    }
}